package org.rosa;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;

public class GradeTracker extends JFrame {
    private JTextField[] gradeFields;
    private JLabel averageLabel;
    private JTextArea reportArea;
    private Map<String, Double> grades;
    private static final String[] subjects = {"Math", "Science", "English", "History"};

    double total = 0;
    double average = 0;

    public GradeTracker(String userType) {
        grades = new HashMap<>();
        gradeFields = new JTextField[subjects.length];

        setTitle("Grade Tracker");
        setSize(400, 400);
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        for (int i = 0; i < subjects.length; i++) {
            gbc.gridx = 0;
            gbc.gridy = i;
            add(new JLabel(subjects[i]), gbc);

            gradeFields[i] = new JTextField(15);
            gbc.gridx = 1;
            add(gradeFields[i], gbc);
        }

        JButton calculateButton = new JButton("Calculate Average");
        gbc.gridx = 0;
        gbc.gridy = subjects.length;
        gbc.gridwidth = 1;
        add(calculateButton, gbc);

        averageLabel = new JLabel();
        gbc.gridx = 1;
        gbc.gridwidth = 1;
        add(averageLabel, gbc);

        calculateButton.addActionListener(new CalculateButtonListener());

        reportArea = new JTextArea(10, 10);
        reportArea.setLineWrap(true);
        reportArea.setWrapStyleWord(true);
        reportArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(reportArea);
        gbc.gridx = 0;
        gbc.gridy = subjects.length + 1;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        add(scrollPane, gbc);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class CalculateButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int count = 0;

            for (int i = 0; i < subjects.length; i++) {
                String input = gradeFields[i].getText();
                if (input.isEmpty()) {
                    continue;
                }

                try {
                    double grade = Double.parseDouble(input);
                    if (grade < 0 || grade > 100) {
                        averageLabel.setText("Invalid input for " + subjects[i]);
                        return;
                    }
                    grades.put(subjects[i], grade);
                    total += grade;
                    count++;
                } catch (NumberFormatException ex) {
                    averageLabel.setText("Invalid input for " + subjects[i]);
                    return;
                }
            }

            if (count == 0) {
                averageLabel.setText("No grades entered.");
                return;
            }

            average = total / count;
            averageLabel.setText("Average: " + average);
            generateReport();
        }
    }

    private String getFeedback() {
        if (average >= 90) {
            return "Excellent!";
        } else if (average >= 75) {
            return "Good job!";
        } else if (average >= 60) {
            return "Needs improvement.";
        } else {
            return "Failing.";
        }
    }

    private void generateReport() {
        StringBuilder report = new StringBuilder();
        for (String subject : subjects) {
            Double grade = grades.get(subject);
            if (grade != null) {
                report.append(subject).append(": ").append(grade).append(" (").append(getLetterGrade(grade)).append(")\n");
            }
        }

        report.append("\nFeedback is ").append(getFeedback());
        reportArea.setText(report.toString());
    }

    private String getLetterGrade(double grade) {
        if (grade >= 90) {
            return "A";
        } else if (grade >= 80) {
            return "B";
        } else if (grade >= 70) {
            return "C";
        } else if (grade >= 60) {
            return "D";
        } else {
            return "F";
        }
    }

    public static void main(String[] args) {
        new GradeTracker("Student");
    }
}